<?php
namespace App\Http\Controllers\Api\Trade;

use Illuminate\Http\Request;
use App\Http\Controllers\Api\Controller;
use App\Http\Resources\Trade\Index\BannerListResource;
use App\Http\Resources\Trade\Index\KindListSupplyResource;
use App\Http\Resources\Trade\Supply\ListResource as SupplyListResource;
use App\Http\Resources\Trade\Demand\ListResource as DemandListResource;
use App\Http\Resources\Trade\Shop\ListResource as ShopListResource;
use App\Services\Trade\SupplyService;
use App\Services\Trade\DemandService;
use App\Services\Trade\ShopService;
use App\Models\Trade\Common\SetBannerModel;
use App\Models\Trade\Common\SetSystemModel;

class IndexController extends Controller
{

    /**
     * 首页Banner
     */
    public function getBanner()
    {
        $data = new BannerListResource(SetBannerModel::selectByShow());
        
        return $this->response->setResponseData($data)->getResponse();
    }

    /**
     * 首页辅助信息（出售热门品种、公司联系电话）
     *
     * @param $page_size 可选            
     */
    public function getInfoSub(Request $request)
    {
        $this->validate($request, [
            'page_size' => 'nullable|integer|between:1,100'
        ]);
        $pageSize = $request->query('page_size', 3);
        
        // 热门品种（出售）
        $supplyService = new SupplyService();
        $data['kindList'] = new KindListSupplyResource($supplyService->selectHotKind(30, $pageSize));
        // 系统信息
        $systemData = SetSystemModel::selectByFirst();
        $data['tel'] = $systemData['contact_tel'] ? json_decode($systemData['contact_tel'], true) : [];
        
        return $this->response->setResponseData($data)->getResponse();
    }

    /**
     * 出售推荐列表
     *
     * @param $page_size 可选            
     */
    public function getSupplyList(Request $request)
    {
        $this->validate($request, [
            'page_size' => 'nullable|integer|between:1,100'
        ]);
        $pageSize = $request->query('page_size', 6);

        $supplyService = new SupplyService();
        
        if (null == ($province = $this->location()->getDistrictProvince())) {
            list ($collection, $count) = $supplyService->selectList(1, $pageSize);
            $data = new SupplyListResource($collection);
        } else {
            list ($collection, $count) = $supplyService->selectList(1, $pageSize, ['province' => $province]);
            if ($count < $pageSize) {
                list ($collectionLocation, $countLocation) = $supplyService->selectList(1, $pageSize - $count, ['id' =>$collection->pluck('id')->all()]);
                
                $data = array_merge(
                    (new SupplyListResource($collection))->toArray($request),
                    (new SupplyListResource($collectionLocation))->toArray($request)
                );
            } else {
                $data = new SupplyListResource($collection);
            }
        }
        
        return $this->response->setResponseData($data)->getResponse();
    }

    /**
     * 求购推荐列表
     *
     * @param $page_size 可选            
     */
    public function getDemandList(Request $request)
    {
        $this->validate($request, [
            'page_size' => 'nullable|integer|between:1,100'
        ]);
        $pageSize = $request->query('page_size', 6);
        
        $demandService = new DemandService();

        if (null == ($province = $this->location()->getDistrictProvince())) {
            list ($collection, $count) = $demandService->selectList(1, $pageSize);
            $data = new DemandListResource($collection);
        } else {
            list ($collection, $count) = $demandService->selectList(1, $pageSize, ['province' => $province]);
            if ($count < $pageSize) {
                list ($collectionLocation, $countLocation) = $demandService->selectList(1, $pageSize - $count, ['id' =>$collection->pluck('id')->all()]);
        
                $data = array_merge(
                    (new DemandListResource($collection))->toArray($request),
                    (new DemandListResource($collectionLocation))->toArray($request)
                );
            } else {
                $data = new DemandListResource($collection);
            }
        }
        
        return $this->response->setResponseData($data)->getResponse();
    }

    /**
     * 店铺推荐列表
     *
     * @param $page_size 可选            
     */
    public function getShopList(Request $request)
    {
        $this->validate($request, [
            'page_size' => 'nullable|integer|between:1,100'
        ]);
        $pageSize = $request->query('page_size', 6);
        
        $shopService = new ShopService();
        list ($collection, $count) = $shopService->selectList(1, $pageSize);
        $data = new ShopListResource($collection);
        
        return $this->response->setResponseData($data)->getResponse();
    }
}
